﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NUnit.Framework;

namespace nzmq.tests.usage_scenarios
{
    [TestFixture]
    public class use_RxExtensions
    {
        [Test]
        public void Publish_an_IObservable()
        {
            using(var ctx = new NzmqContext())
            using(var psck = ctx.CreatePublisherSocket("inproc://pub"))
            using(var ssck = ctx.CreateSubscriberSocket("inproc://pub"))
            {
                var s = new Subject<string>();
                s.Publish(psck);

                s.OnNext("hello");

                Assert.AreEqual("hello", ssck.Receive());
            }
        }


        [Test]
        public void Subscribe_to_a_socket()
        {
            using (var ctx = new NzmqContext())
            using (var psck = ctx.CreatePublisherSocket("inproc://pub"))
            {
                var received = new List<string>();

                ctx.CreateObservable<string>("inproc://pub")
                    .Where(m => m.IndexOf("4") >= 0)
                    .Subscribe(received.Add);

                for(var i=0; i<20; i++)
                    psck.Broadcast(i.ToString());

                Thread.Sleep(300);
                Assert.AreEqual(new[]{"4", "14"}, received.ToArray());
            }
        }
    }
}
